<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class StaticManager{
	private $db = null;

	function __construct($db)
	{
		$this->db = $db;
	}

	function getAboutOurStore()
	{
		$result = $this->getByValue('About Our Store');
		return $result;
	}

	function getAboutUs()
	{
		$result = $this->getByValue('About Us');
		return $result;
	}

	function getServices()
	{
		$result = $this->getByValue('Services');
		return $result;
	}

	function getPrivacy()
	{
		$result = $this->getByValue('Privacy');
		return $result;
	}

	public function getLimit($startIndex = 0, $num = 0)
	{
		$query = "SELECT *
				  FROM static AS s, entity AS e 
				  WHERE s.nid = e.nid AND e.status = 1 AND e.type = 'static'
				  LIMIT $startIndex, $num";		
		return $this->db->get_data($query, "static");
	}

	public function countStatic()
	{
		$query = "SELECT COUNT(s.nid) AS num
				  FROM static AS s, entity AS e
				  WHERE s.nid = e.nid AND e.status = 1 AND e.type = 'static'";//, e.status = 1";
		$result = $this->db->dbQuery($query);
		$row = mysql_fetch_assoc($result);
		$num = $row['num'];
		return $num;
	}

	public function setStatusById($id, $st)
	{
		$query = "UPDATE entity
				  SET status = $st
				  WHERE nid = $id";
		$this->db->dbQuery($query);
	}

	public function getNow()
	{
		if ($this->db != null) {
			//$this->db->startConnection();

			$result = $this->db->dbQuery("SELECT NOW() AS nowTime;");


			$row = mysql_fetch_assoc($result);
			$now = $row['nowTime'];
			//$this->db->closeConnection();
			return $now;
		}
	}

	public function getMaxId()
	{
		//$this->db->startConnection();
		$query = "SELECT MAX(nid) AS nid FROM entity AS e WHERE e.status = 1";
		$arrResult = $this->db->dbQuery($query);
		$row = mysql_fetch_assoc($arrResult);
		//$this->db->closeConnection();
		return $row['nid'];

	}

	/*delete permanently*/
	public function delStaticById($id)
	{
		if ($this->db != null) {
			$this->db->dbQuery("DELETE FROM static WHERE nid = $id ");
			$this->db->dbQuery("DELETE FROM entity WHERE nid = $id ");
		}
	}

	public function getStaticById($id){
		$query = "SELECT *
				  FROM entity AS e, static AS s
				  WHERE s.nid = e.nid AND s.nid = $id AND e.status = 1";
		//echo $query;
		$result = $this->db->get_data($query, 'static');
		return $result;
	}

	public function getByValue($value) {
		$query = "SELECT *
				  FROM static AS s, entity AS e
				  WHERE s.nid = e.nid AND e.status = 1 AND s.value = " . var_export($value, true);
		//echo $query;
		$array = $this->db->get_data($query, 'static');
		if (count($array) == 0) {
			//echo "count = ".count($array);
			$query = "SELECT *
					  FROM static
					  WHERE value = 'Default Page'";
			$array = $this->db->get_data($query, 'static');
			//echo "count = ".count($array);
		}
		///else echo "success ";
		return $array;
	}
}
?>